package com.wlgzs.booksystem.controller.back;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wlgzs.booksystem.entity.Book;
import com.wlgzs.booksystem.entity.Category;
import com.wlgzs.booksystem.enums.Result;
import com.wlgzs.booksystem.enums.ResultCodeEnum;
import com.wlgzs.booksystem.service.CategoryService;
import com.wlgzs.booksystem.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wlgzs-sjl
 * @since 2020-11-17
 */
@RestController
@RequestMapping("/back/category")
@Api(tags = "类别管理")
public class CategoryController {
    @Autowired
    CategoryService categoryService;
    @Autowired
    FileService fileService;

    @Value("${qiniu.path}")
    private String path;

    @GetMapping
    @ApiOperation(value = "后台类别管理主页",httpMethod = "GET", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页(默认为1)", defaultValue = "1",required = true, dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每一页的大小(默认为10)", defaultValue = "5",required = true, dataType = "int")
    })
    public Result categoryIndex(@RequestParam(defaultValue = "1") int current, @RequestParam(defaultValue = "10") int size){
        Page<Category> page=new Page<>(current,size);
        List<Category> categories=categoryService.page(page).getRecords();
        return new Result(ResultCodeEnum.SUCCESS,categories);
    }

    @GetMapping("/updateIndex")
    @ApiOperation(value = "后台修改类别页面",httpMethod = "GET", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryId", value = "类别id", required = true, dataType = "int")
    })
    public Result updateIndex(Integer categoryId){
        Category category=categoryService.getById(categoryId);
        return new Result(ResultCodeEnum.SUCCESS,category);
    }

    @GetMapping("/findByName")
    @ApiOperation(value = "根据类别名查找类别",httpMethod = "GET", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "类别名", required = true, dataType = "String")
    })
    public Result findCategoryByName(String name){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.like("category_name",name);
        List<Category> categories=categoryService.list(wrapper);
        if (categories!=null){
            return new Result(ResultCodeEnum.FIND,categories);
        }else {
            return new Result(ResultCodeEnum.UNFIND);
        }
    }

    @DeleteMapping
    @ApiOperation(value = "删除类别",httpMethod = "DELETE", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryId", value = "类别id", required = true, dataType = "int")
    })
    public Result deleteCategoryById(int categoryId){
        if (categoryService.removeById(categoryId)){
            return new Result(ResultCodeEnum.DELETE);
        }else {
            return new Result(ResultCodeEnum.UNDELETE);
        }
    }

    @PostMapping
    @ApiOperation(value = "添加或修改类别",httpMethod = "POST", response = Result.class)
    public Result insertBook(Category category,MultipartFile upfile){
        if (upfile!=null){
            try{
                String imageName = fileService.uploadFile(upfile);
                category.setPicture(path+"/"+imageName);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        if (categoryService.saveOrUpdate(category)){
            return new Result(ResultCodeEnum.SUCCESS);
        }else {
            return new Result(ResultCodeEnum.FAIL);
        }
    }
}

